package gui;

import database.*;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.List;
import java.util.Vector;


import javax.swing.*;

import javax.swing.table.DefaultTableModel;

public class PanelAlleRetter extends JPanel implements ActionListener {
	private JScrollPane scrollPane;
	private DefaultTableModel tableModel;
	private JTable tabell;
	private JButton  buttonHovedmeny, buttonTilbake;
	private Vector<String> kolonnenavn;
	private GridBagConstraints constraints;
	private JLabel labelRetter;
	
	public PanelAlleRetter(){
		this.setLayout(new GridBagLayout());
		this.leggTilKomponenter();
		GUI.getVindu().add(this);
		
	}
	private void leggTilKomponenter(){
		kolonnenavn = getKolonnenavn();
		tableModel = new DefaultTableModel(kolonnenavn,0){
			@Override
			public boolean isCellEditable(int row, int column) {
			       //all cells false
			       return false;
			}
		};
		fyllTableModel();
		
		
		labelRetter = new JLabel("Alle retter");
		Font f = new Font(labelRetter.getFont().getName(), labelRetter.getFont().getStyle(), 30);
		labelRetter.setFont(f);
		
		constraints = new GridBagConstraints();
		
		buttonHovedmeny = new JButton("Tilbake til hovedmeny");
		buttonHovedmeny.addActionListener(this);
		
		buttonTilbake = new JButton("Tilbake");
		buttonTilbake.addActionListener(this);
		
		tabell = new JTable(tableModel);
		tabell.getColumnModel().getColumn(0).setPreferredWidth(50);
		tabell.getColumnModel().getColumn(1).setPreferredWidth(80);
		tabell.getColumnModel().getColumn(2).setPreferredWidth(240);
		tabell.getColumnModel().getColumn(3).setPreferredWidth(50);
		
		scrollPane = new JScrollPane(tabell);
		scrollPane.setMinimumSize(new Dimension(600, 400));
		scrollPane.setPreferredSize(new Dimension(600, 400));

		
		
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.weightx = 1;
		constraints.gridwidth = 2;
		this.add(labelRetter,constraints);
		constraints.gridy = 1;
		
		this.add(scrollPane,constraints);
		constraints.gridwidth = 1;
		constraints.gridy = 2;
		constraints.gridx = 0;
		constraints.anchor = GridBagConstraints.LINE_END;
		this.add(buttonHovedmeny,constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.LINE_START;
		this.add(buttonTilbake, constraints);
		
	}

	private void buttonHovedmeny_actionPerformed(ActionEvent e) 
	{
		this.setVisible(false);
		new PanelHovedmeny();
	}
	private void buttonTilbake_actionPerformed(ActionEvent e) 
	{
		this.setVisible(false);
		new PanelNyRett();
	}

	private Vector<String> getKolonnenavn(){
		Vector<String> kolonnenavn = new Vector<String>();
		kolonnenavn.add("Nummer");
		kolonnenavn.add("Navn");
		kolonnenavn.add("Beskrivelse");
		kolonnenavn.add("Pris");
		return kolonnenavn;
	}
	private void fyllTableModel(){
		List<Rett> retter = Databasecon.getMeny();
		for (int i = 0; i < retter.size(); i++) {
			Vector<String> radData = new Vector<String>();
			radData.add(Integer.toString(retter.get(i).getVareID()));
			radData.add(retter.get(i).getNavn());
			radData.add(retter.get(i).getBeskrivelse());
			radData.add(Integer.toString(retter.get(i).getPris()));
			tableModel.addRow(radData);
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buttonHovedmeny)
			buttonHovedmeny_actionPerformed(e);
		else if(e.getSource()==buttonTilbake)
			buttonTilbake_actionPerformed(e);
		
	}
}
